package com.lc.guessTheWords.rank;

import com.lc.guessTheWords.MainActivity;
import com.lc.guessTheWords.rank.SubmitScoreAsyncTask.Param;
import com.lc.guessTheWords.util.GameUtil;

public class SAEHelper {
	public static final String SAE_SERVER_URL = "http://1.guessthewords.sinaapp.com/";
	public static final String SUBMIT_SCORE_URL = "/submitScore.php";
	public static final String GET_TOP10_URL = "/getTop10Rank.php";
	public static final String GET_RANK_IN_RANGE_URL = "/getRankInRange.php";
	public static final String GET_MY_RANK_URL = "/getMyRank.php";
	public static final String PARAM_ID = "id";
	public static final String PARAM_NAME = "name";
	public static final String PARAM_TYPE = "type";
	public static final String PARAM_SCORE = "score";
	public static final String PARAM_START = "start";
	public static final String PARAM_COUNT = "count";

	private static void showNoInternetTip() {
		MainActivity.instance.showTip("无法连接到网络，请检查网络设置");
	}

	public static void submitScore(final String id, final String name,
			final int type, final long score) {
		if (score > 0) {
			if (!GameUtil.canAccessInternet()) {
				showNoInternetTip();
				return;
			}
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					SubmitScoreAsyncTask task = new SubmitScoreAsyncTask();
					Param param = task.new Param();
					param.id = id;
					param.name = name;
					param.type = type;
					param.score = score;
					task.execute(param);
				}
			});
		}
	}

	public static void getTop10Rank(final int type) {
		if (!GameUtil.canAccessInternet()) {
			return;
		}
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				GetTop10RankAsyncTask task = new GetTop10RankAsyncTask();
				task.execute(type);
			}
		});
	}

	public static void getMyRank(final int type) {
		if (!GameUtil.canAccessInternet()) {
			return;
		}
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// 先获得本人的排名，再获取周围人的排名
				GetMyRankAsyncTask task = new GetMyRankAsyncTask();
				task.execute(type);
			}
		});
	}
}
